﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rezene.Web.Business;
using Rezene.Web.Business.Amazon;
using Rezene.Web.Models;
using System.Web.Security;

namespace Rezene.Web.Controllers
{
    public class EditorController : BaseController
    {
        public ActionResult LoginLogoutLink()
        {
            return PartialView(IsAuthorized);
        }

        public ActionResult Login()
        {
            return View(new LoginViewModel());
        }

        [AllowAnonymous]
        [HttpPost]
        public ActionResult Login(LoginViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (model.Username == ConfigurationRepository.GetAdminUsername() && model.Password == ConfigurationRepository.GetAdminPassword())
                    {
                        FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
                        if (string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
                            return RedirectToAction("List", "Gallery");
                        else
                            return Redirect(Request.QueryString["ReturnUrl"]);
                    }
                    else
                    {
                        ModelState.AddModelError("LoginError", "Login failed");
                    }

                }
                return View(model);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [Authorize]
        public ActionResult Logout()
        {
            if (Request.IsAuthenticated)
                FormsAuthentication.SignOut();
            return RedirectToAction("List", "Gallery");
        }

        [Authorize]
        public ActionResult EditGallery(string path)
        {
            var gallery = AmazonRepository.GetGalleryInfo(path);
            if (gallery != null)
            {
                var model = new EditGalleryViewModel();
                model.Name = gallery.Name;
                model.Show = gallery.Show;
                model.Title = gallery.Title;
                model.Path = gallery.AmazonPath;
                return View(model);
            }
            return new HttpNotFoundResult();
        }

        [HttpPost]
        [Authorize]
        public ActionResult EditGallery(EditGalleryViewModel model)
        {
            if (ModelState.IsValid)
            {
                var gallery = AmazonRepository.GetGalleryInfo(model.Path);
                if (gallery != null)
                {
                    gallery.Show = model.Show;
                    gallery.Title = model.Title;
                    AmazonRepository.SaveGalleryInfo(gallery);
                    return RedirectToAction("List", "Gallery", new { id = gallery.AmazonPath });
                }
            }

            return View(model);
        }

        [Authorize]
        public ActionResult EditPhoto(string path)
        {
            var photo = AmazonRepository.GetPhotoInfo(path);
            if (photo != null)
            {
                var model = new EditPhotoViewModel
                {
                    Name = photo.Name,
                    Show = photo.Show,
                    Title = photo.Title,
                    Path = photo.AmazonPath,
                    Description = photo.Description
                };
                return View(model);
            }
            return new HttpNotFoundResult();
        }

        [HttpPost]
        [Authorize]
        public ActionResult EditPhoto(EditPhotoViewModel model)
        {
            if (ModelState.IsValid)
            {
                var photo = AmazonRepository.GetPhotoInfo(model.Path);
                if (photo != null)
                {
                    photo.Show = model.Show;
                    photo.Title = model.Title;
                    photo.Description = model.Description;
                    AmazonRepository.SavePhotoInfo(photo);
                    return RedirectToAction("List", "Gallery", new { id = photo.GalleryPath });
                }
            }

            return View(model);
        }
    }
}

